<template>
  <div class="goods-container">
    <el-card class="box-card">
      <!-- 表格 -->
      <el-table :data="goodsList" style="width: 100%" stripe  lazy @selection-change="handleSelectionChange">>
        <el-table-column
      type="selection"
      width="55">
    </el-table-column>
        <el-table-column label="商品图片" width="180" align="center">
          <template v-slot="slot">
            <el-image
              :preview-src-list="[slot.row.goods_img]"
              :src="slot.row.goods_img"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="aut_name" label="作者" width="80" align="center">
        </el-table-column>
        <el-table-column
          prop="createdAt"
          label="发布时间"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="stutas" label="状态" width="80" align="center">
          <template v-slot="slot">
            <el-tag type="success" v-if="slot.row.stutas">已发布</el-tag>
            <el-tag v-else>草稿箱</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="channel_id"
          label="分类"
          width="80"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="address" label="操作" width="180" align="center">
          <template v-slot="slot">
            <el-button type="primary" icon="el-icon-edit" size="small"
            @click="editClick(slot.row)"
              >编辑</el-button
            >
            <el-button type="danger" icon="el-icon-delete" size="small"
             @click="deleteClick(slot.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
      background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getGoodsList,deleteGoods } from "@/api/goods";
export default {
  name:'Goods',
  data() {
    return {
      goodsList: [],// 商品表格数据
       queryInfo: {
        query: '', // 查询参数
        pageNum: 1,// 分页页码
        pageSize: 10// 页面数据条数
      },
      total: 0,
      multipleSelection: []// 全选数据
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    // 文章表格数据
    async getGoodsList() {
      const { list, pageNum, pageSize, total } = await getGoodsList(this.queryInfo);
      this.goodsList = list;
      this.queryInfo.pageNum = Number(pageNum);
      this.queryInfo.pageSize = Number(pageSize);
      this.total = total;
    },
    // 设置页面数据条数
    handleSizeChange(val) {
      this.queryInfo.pageSize = val
      this.getGoodsList();
    },
    // 设置分页页码
    handleCurrentChange(val) {
      this.queryInfo.pageNum=val
      this.getGoodsList();
    },
    // 编辑用户
    editClick(row){
      this.$message({
          message: '该功能未实现',
          type: 'warning'
        });
    },
    // 删除用户
    deleteClick(row){
     this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
           await deleteGoods(row.id)
           this.getGoodsList();
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    // 全选数据
    handleSelectionChange(val) {
      this.multipleSelection=[]
        val.map(item => {
          this.multipleSelection.push(item.id)
        });
      }
  },
};
</script>

<style lang="scss">
.box-card {
  margin: 20px;
}
.el-table{
  margin-bottom: 20px;
}
.el-image{
  border-radius: 5px;
}
</style>
